home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / lha_axeman / shuf.c < prev    next >
C/C++ Source or Header  |  1995-09-01  |  3KB  |  183 lines

  1. /***********************************************************
  2.     shuf.c -- extract static Huffman coding
  3. ***********************************************************/
  4. #include <stdio.h>
  5. #include "slidehuf.h"
  6. #ifdef __STDC__
  7. #include <stdlib.h>
  8. #endif
  9.  
  10. #define N1 286  /* alphabet size */
  11. #define N2 (2 * N1 - 1)  /* # of nodes in Huffman tree */
  12. #define EXTRABITS 8
  13. #define BUFBITS  16  /* >= log2(MAXBUF) */
  14. #define LENFIELD  4  /* bit size of length field for tree output */
  15. #define NP (8 * 1024 / 64)
  16. #define NP2 (NP * 2 - 1)
  17.  
  18. static unsigned int np;
  19.  
  20. void decode_start_st0(void)
  21. {
  22.     n_max = 286;
  23.     maxmatch = MAXMATCH;
  24.     init_getbits();
  25.     np = 1 << (MAX_DICBIT - 6);
  26. }
  27.  
  28. void encode_p_st0(unsigned short j)
  29. {
  30.     unsigned short i;
  31.  
  32.     i = j >> 6;
  33.     putcode(pt_len[i], pt_code[i]);
  34.     putbits(6, j & 0x3f);
  35. }
  36.  
  37. int fixed[2][16] =
  38. {
  39.     {3, 0x01, 0x04, 0x0c, 0x18, 0x30, 0},                /* old compatible */
  40.     {2, 0x01, 0x01, 0x03, 0x06, 0x0D, 0x1F, 0x4E, 0}    /* 8K buf */
  41. };
  42.  
  43. static void ready_made(int method)
  44. {
  45.     int i, j;
  46.     unsigned int code, weight;
  47.     int *tbl;
  48.  
  49.     tbl = fixed[method];
  50.     j = *tbl++;
  51.     weight = 1 << (16 - j);
  52.     code = 0; 
  53.     for (i = 0; i < np; i++)
  54.   {
  55.         while (*tbl == i)
  56.     {
  57.             j++;
  58.             tbl++;
  59.             weight >>= 1;
  60.         }
  61.         pt_len[i] = j;
  62.         pt_code[i] = code;
  63.         code += weight;
  64.     }
  65. }
  66.  
  67. void encode_start_fix(void)
  68. {
  69.     n_max = 314;
  70.     maxmatch = 60;
  71.     np = 1 << (12 - 6);
  72.     init_putbits();
  73.     start_c_dyn();
  74.     ready_made(0);
  75. }
  76.  
  77. static void read_tree_c(void)  /* read tree from file */
  78. {
  79.     int i, c;
  80.  
  81.     i = 0;
  82.     while (i < N1)
  83.   {
  84.         if(getbits(1))
  85.       c_len[i] = getbits(LENFIELD) + 1;
  86.         else
  87.       c_len[i] = 0;
  88.         if (++i == 3 && c_len[0] == 1 && c_len[1] == 1 && c_len[2] == 1)
  89.     {
  90.             c = getbits(CBIT);
  91.             for (i = 0; i < N1; i++) c_len[i] = 0;
  92.             for (i = 0; i < 4096; i++) c_table[i] = c;
  93.             return;
  94.         }
  95.     }
  96.     make_table(N1, c_len, 12, c_table);
  97. }
  98.  
  99. static void read_tree_p(void)  /* read tree from file */
  100. {
  101.     int i, c;
  102.  
  103.     i = 0;
  104.     while(i < NP)
  105.   {
  106.         pt_len[i] = getbits(LENFIELD);
  107.         if(++i == 3 && pt_len[0] == 1 && pt_len[1] == 1 && pt_len[2] == 1)
  108.     {
  109.             c = getbits(MAX_DICBIT - 6);
  110.             for (i = 0; i < NP; i++) c_len[i] = 0;
  111.             for (i = 0; i < 256; i++) c_table[i] = c;
  112.             return;
  113.         }
  114.     }
  115. }
  116.  
  117. void decode_start_fix(void)
  118. {
  119.     n_max = 314;
  120.     maxmatch = 60;
  121.     init_getbits();
  122.     np = 1 << (12 - 6);
  123.     start_c_dyn();
  124.     ready_made(0);
  125.     make_table(np, pt_len, 8, pt_table);
  126. }
  127.  
  128. unsigned short decode_c_st0(void)
  129. {
  130.     int i, j;
  131.     static unsigned short blocksize = 0;
  132.  
  133.     if(blocksize == 0)
  134.   {
  135.         blocksize = getbits(BUFBITS);  /* read block blocksize */
  136.         read_tree_c();
  137.         if(getbits(1))
  138.     {
  139.             read_tree_p();
  140.         }
  141.     else
  142.     {
  143.             ready_made(1);
  144.         }
  145.         make_table(NP, pt_len, 8, pt_table);
  146.     }
  147.     blocksize--;
  148.     j = c_table[bitbuf >> 4];
  149.     if(j < N1) fillbuf(c_len[j]);
  150.     
  151.     else {
  152.         fillbuf(12); i = bitbuf;
  153.         do {
  154.             if ((short)i < 0) j = right[j];
  155.             else              j = left [j];
  156.             i <<= 1;
  157.         } while (j >= N1);
  158.         fillbuf(c_len[j] - 12);
  159.     }
  160.     if (j == N1 - 1)
  161.         j += getbits(EXTRABITS);
  162.     return j;
  163. }
  164.  
  165. unsigned short decode_p_st0(void)
  166. {
  167.     int i, j;
  168.  
  169.     j = pt_table[bitbuf >> 8];
  170.     if (j < np) {
  171.         fillbuf(pt_len[j]);
  172.     } else {
  173.         fillbuf(8); i = bitbuf;
  174.         do {
  175.             if ((short)i < 0) j = right[j];
  176.             else              j = left [j];
  177.             i <<= 1;
  178.         } while (j >= np);
  179.         fillbuf(pt_len[j] - 8);
  180.     }
  181.     return (j << 6) + getbits(6);
  182. }
  183.